package cvo.pro4.chess.client.pocDragDrop;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import cvo.pro4.chess.client.entities.*;
import cvo.pro4.chess.client.exceptions.IllegalCoordinatesException;

public class BasicWidget extends Composite{
	private Game game;

	private static BasicWidgetgetUiBinder uiBinder = GWT
			.create(BasicWidgetgetUiBinder.class);

	interface BasicWidgetgetUiBinder extends UiBinder<Widget, BasicWidget> {
	}

	public BasicWidget() {
		initWidget(uiBinder.createAndBindUi(this));
	}

	//elk field van op de xml file moet je hier ook @UiField toewijzen. Dit is
	//de link tussen uw java doc en uw xml file.
	@UiField
	protected Button button;
	@UiField
	protected TextBox txttextbox;
	@UiField
	protected Label title;
	@UiField
	protected Label result;
	@UiField
	protected Button button2;
	@UiField
	protected TextBox txttextbox2;
	@UiField
	protected Label title2;
	@UiField
	protected Label result2;

	public BasicWidget(String buttontext, String title, Game game) {
		initWidget(uiBinder.createAndBindUi(this));
		this.setTextButton(buttontext);
		this.setTextTitle(title);
		this.game = game;
	}

	@UiHandler("button")
	void onClick(ClickEvent e) throws IllegalCoordinatesException {
		Piece p = game.getBoard().getPieceAtLocation(BoardCoordinates.create(this.txttextbox.getText()));
		if (p==null){
			this.result.setText("geen stuk gevonden op veld " + this.txttextbox.getText());
		}
		this.result.setText("op veld " + this.txttextbox.getText() + " staat het volgende stuk: " + p.toString());
	}	
	
	@UiHandler("button2")
	void onClick2(ClickEvent e) throws IllegalCoordinatesException {
		BoardCoordinates bc = BoardCoordinates.create(this.txttextbox.getText());
		Piece p = game.getBoard().getPieceAtLocation(BoardCoordinates.create(this.txttextbox.getText()));
		if (bc==null){
			this.result2.setText("Geen geldig veld ingevoerd!");
		}
		p.move(bc);
		this.result2.setText(p.toString() + " staat nu op " + bc.toString());
	}	
	
	
	//hier kan je een naam sette aan de button of label
	public void setTextButton(String text) {
		this.button.setText(text);
	}
	
	public void setTextTitle(String titletext){
		this.title.setText(titletext);
	}

	//de naam die je geeft aan de button of label
	public String getTextButton() {
		return this.button.getText();
	}
	
	public String getTextTitle(){
		return this.title.getText();
	}
	
	public void setTextResult(String text){
		
		this.result.setText(text);
	}
	
	public String getTextResult(){
		return this.result.getText();
	}


}
